package com.es.disped.web.controller.level;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.es.disped.api.level.LevelService;
import com.es.disped.common.constant.RecordStatus;
import com.es.disped.common.model.MsgModel;
import com.es.disped.common.model.PageModel;
import com.es.disped.core.controller.BaseController;
import com.es.disped.dao.model.Level;
import com.es.disped.dao.model.LevelExample;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(value="等级阶段管理")
@Controller
@RequestMapping("/manage/level")
public class LevelController extends BaseController {

	@Autowired
	LevelService levelService;
	
	@ApiOperation(value="保存等级",response=MsgModel.class,httpMethod="POST",
			notes="保存等级对象，等级名称不能为空")
	@RequiresPermissions(value="sys:save")
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public @ResponseBody MsgModel save(@RequestBody @ApiParam(name="level",value="等级对象",required=true) Level level)
	{
		int count=0;
		if(Optional.of(level)
				.filter(name->level.getLevelName()!=null)
				.isPresent())
		{
			level.setLevelId(this.getUUID());
			level.setLevelValid(RecordStatus.USEAGE.getValue());
			count=levelService.insertSelective(level);
		}
		return new MsgModel(count==1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count==1?"保存成功":"保存失败", count==1?level:"") ;
	}
	
	@ApiOperation(value="更新等级",response=MsgModel.class,httpMethod="POST",
			notes="更新等级对象，等级ID不能为空")
	@RequiresPermissions(value="sys:update")
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public @ResponseBody MsgModel update(@RequestBody @ApiParam(name="level",value="等级对象",required=true) Level level)
	{
		int count=0;
		if(Optional.of(level)
				.filter(name->level.getLevelId()!=null)
				.isPresent())
		{
			count=levelService.updateByPrimaryKey(level);
		}
		return new MsgModel(count==1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count==1?"更新成功":"更新失败", count==1?level:"") ;
	}
	
	@ApiOperation(value="删除等级",response=MsgModel.class,httpMethod="POST",
			notes="删除等级对象，等级ID不能为空")
	@RequiresPermissions(value="sys:delete")
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public @ResponseBody MsgModel delete(@RequestBody @ApiParam(name="level",value="等级对象",required=true) Level level)
	{
		int count=0;
		if(Optional.of(level)
				.filter(name->level.getLevelId()!=null)
				.isPresent())
		{
			count=levelService.deleteByPrimaryKey(level.getLevelId());
		}
		return new MsgModel(count==1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count==1?"删除成功":"删除失败", count==1?level:"") ;
	}
	
	@ApiOperation(value="检索等级",response=MsgModel.class,httpMethod="POST",notes="检索等级，包括模糊检索（等级名称,等级描述），精确检索（等级ID）")
	@RequestMapping(value="/list",method=RequestMethod.POST)
	public @ResponseBody MsgModel list(@RequestBody @ApiParam(name="level",value="等级对象",required=false) Level level)
	{
		LevelExample listExample=new LevelExample();
		if(!this.isNull(level.getLevelId()))
		{
			listExample.createCriteria().andLevelIdEqualTo(level.getLevelId());
		}
		if(!this.isNull(level.getLevelName()))
		{
			listExample.createCriteria().andLevelNameLike("%"+level.getLevelName()+"%");
		}
		if(!this.isNull(level.getLevelDescription()))
		{
			listExample.createCriteria().andLevelDescriptionLike("%"+level.getLevelDescription()+"%");
		}
		List<Level> levels=levelService.selectByExample(listExample);
		return this.resultMsg(!this.isNull(levels)?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				!this.isNull(levels)?"等级列表加载成功":"等级列表为空", !this.isNull(levels)?levels:"");
	}
	
	
	@ApiOperation(value="获取等级列表",httpMethod="POST",response=MsgModel.class,notes="获取等级信息（管理端），分页加载")
	@RequestMapping(value="/pages",method=RequestMethod.POST)
	public @ResponseBody MsgModel pages(@RequestBody @ApiParam(name="paramMap",value="分页参数[offset,limit]",required=true) Map<String, Object> paramMap)
	{
		LevelExample pageExample=new LevelExample();
		PageModel<Level> pageModel=null;
		if(Optional.of(paramMap)
		.filter(offset->paramMap.containsKey("offset")!=false)
		.filter(limit->paramMap.containsKey("limit")!=false)
		.isPresent())
		{
			pageModel=levelService.selectByExampleForOffsetPage(pageExample,
					Integer.valueOf(paramMap.get("offset").toString()), Integer.valueOf(paramMap.get("limit").toString()));
		}
		return this.resultMsg(pageModel!=null?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				pageModel!=null?"等级列表加载成功":"等级列表为空", pageModel!=null?pageModel:"");
	}
}
